package com.slink.xin.framework.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 字符串工具类
 * 
 * @author xin
 *
 */
public class StringUtil {
	public static List<String> lineInput(String str) {
		if (str == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] array = str.split("\n");
		for (String s : array) {
			if (s != null && !s.isEmpty()) {
				list.add(s.trim());
			}
		}
		return list;
	}

	public static String lineInputHTML(String str) {
		if (str == null)
			return "";
		str = str.replaceAll("\n", "<br/>");
		str = str.replaceAll(" ", "&nbsp;");
		return str;
	}

	public static String convertHTML(String str) {
		if (str == null)
			return "";
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("<", "&lt;");
		return str;
	}

	/**
	 * Encoding a string by MD5.
	 *
	 * @param key
	 *            The string need to be encoded.
	 * @return The resultant string.
	 */
	public static String getStringMD5(String key) {
		String value = null;
		MessageDigest currentAlgorithm;
		try {
			currentAlgorithm = MessageDigest.getInstance("MD5");
			currentAlgorithm.reset();
			currentAlgorithm.update(key.getBytes("GBK"));
			byte[] hash = currentAlgorithm.digest();
			String d = "";
			int usbyte = 0; // unsigned byte
			for (int i = 0; i < hash.length; i += 2) { // format with 2-byte
				// words with spaces.
				usbyte = hash[i] & 0xFF; // byte-wise AND converts signed
											// byte
				// to unsigned.
				if (usbyte < 16)
					d += "0" + Integer.toHexString(usbyte); // pad on left if
				// single hex digit.
				else
					d += Integer.toHexString(usbyte);
				usbyte = hash[i + 1] & 0xFF; // byte-wise AND converts signed
				// byte to unsigned.
				if (usbyte < 16)
					d += "0" + Integer.toHexString(usbyte);// + " "; // pad on
				// left if single hex
				// digit.
				else
					d += Integer.toHexString(usbyte);// + " ";
			}
			// return d.toUpperCase();
			value = d.trim().toLowerCase();

		} catch (NoSuchAlgorithmException e) {
			System.out.println("MD5 algorithm not available.");
		} catch (UnsupportedEncodingException e) {
			System.out.println("MD5 algorithm not available.");
		}
		return value;
	}

	/**
	 * URL编码
	 * 
	 * @param str
	 * @return
	 */
	public static String encode(String str) {
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * URL解码
	 * 
	 * @param str
	 * @return
	 */
	public static String decode(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 判空字符串
	 * 
	 * @param src
	 *            true代表空
	 * @return
	 */
	public static boolean isBlank(String src) {
		if (src == null || "".equals(src)) {
			return true;
		}

		return false;
	}

	/**
	 * 判非空字符串
	 * 
	 * @param src
	 *            true代表非空
	 * @return
	 */
	public static boolean isNotBlank(String src) {
		return !isBlank(src);
	}

	/**
	 * 字符串连接
	 * 
	 * @param join
	 *            连接符，默认逗号(join为空)
	 * @param strs
	 *            被连接的字符串
	 * @return
	 */
	public static String join(String join, String... strs) {
		StringBuilder sb = new StringBuilder();
		join = StringUtil.isBlank(join) ? "," : join;
		for (int i = 0, len = strs.length; i < len; i++) {
			if (i > 0) {
				sb.append(join);
			}

			sb.append(strs[i]);
		}

		return sb.toString();
	}

	/**
	 * 字符串连接
	 * 
	 * @param join
	 *            连接符，默认逗号(join为空)
	 * @param strs
	 *            被连接的字符串
	 * @return
	 */
	public static String join(String join, List<String> list) {
		StringBuilder sb = new StringBuilder();
		join = StringUtil.isBlank(join) ? "," : join;

		if (list != null) {
			for (int i = 0, len = list.size(); i < len; i++) {
				if (i > 0) {
					sb.append(join);
				}

				sb.append(list.get(i));
			}
		}

		return sb.toString();
	}
}
